package com.ckf.springboot_mysql_redis.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckf.springboot_mysql_redis.entity.Users;
import com.ckf.springboot_mysql_redis.model.ResultFormat;
import com.ckf.springboot_mysql_redis.model.ResultFormatPaging;
import com.ckf.springboot_mysql_redis.service.UsersService;
import com.ckf.springboot_mysql_redis.utils.ResultPagingUtil;
import com.ckf.springboot_mysql_redis.utils.ResultUtil;
import com.ckf.springboot_mysql_redis.utils.TimeUtitl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 安详的苦丁茶
 * @since 2020-05-02
 */
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/users")
public class UsersController {

    private Logger logger = LoggerFactory.getLogger(UsersController.class);


    @Autowired
    private UsersService usersService;


    /**
     * 分页全查询用户信息
     *
     * @return
     */
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true, dataType = "Integer", defaultValue = "5")
    })

    /**
     * 全查询分类信息
     *
     * @return
     */
    @ApiOperation("查询用户信息接口")
    @GetMapping("/list")
    public ResultFormatPaging pgUsersList(Users users, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        logger.info("-------------------进入用户查询controller层--------------------------");
        logger.info("users={}", users);
        IPage<Users> usersIPage = usersService.selectUsersList(users, page, limit);
        List<Users> usersList = usersIPage.getRecords();
        logger.info("usersList={}", usersList);
        return ResultPagingUtil.pagingSuccess(0, usersIPage.getTotal(), usersList);

    }


    /**
     * 添加用户信息
     *
     * @param users
     * @return
     */
    @ApiOperation("添加用户信息接口")
    @PostMapping("/save")
    @ResponseBody
    public ResultFormat savePgSorts(Users users) {
        users.setRegisterTime(TimeUtitl.dateTime());
        users.setUpdateTime(TimeUtitl.dateTime());

        ResultFormat insert = usersService.saveUsers(users);

        if (insert.getCode() == 200) {
            logger.info("添加成功");
            return ResultUtil.success();
        }
        if (insert.getCode() == 101) {
            logger.info("用户名已存在");
            return ResultUtil.error(insert.getCode(), insert.getMsg());
        }

        return ResultUtil.error(100, "添加失败");
    }


    /**
     * 修改用户信息
     *
     * @param users
     * @return
     */
    @ApiOperation("修改用户信息接口")
    @PutMapping("/update")
    @ResponseBody
    public ResultFormat updatePgSorts(Users users) {

        users.getRegisterTime();
        users.setUpdateTime(TimeUtitl.dateTime());

        Integer insert = usersService.updateUsers(users);

        if (insert == 101) {
            logger.info("用户名已存在");
            return ResultUtil.error(101, "用户名已经存在");
        }
        if (insert == 200) {
            logger.info("修改成功");
            return ResultUtil.success();
        }

        return ResultUtil.error(100, "修改失败");

    }


    /**
     * 修改用户状态
     *
     * @param users
     * @return
     */
    @ApiOperation("修改用户状态接口")
    @PutMapping("/delete")
    public ResultFormat deletePgSorts(Users users) {

        logger.info("users=={}", users);

        boolean flag = usersService.updateById(users);

        if (flag) {
            logger.info("修改成功");
            return ResultUtil.success();
        } else {
            logger.info("修改失败");
            return ResultUtil.error(100, "修改失败");
        }
    }


    /**
     * 批量删除用户信息
     * 状态为1 :已删除
     *
     * @param state
     * @param userId
     * @return
     */
    @PutMapping("delBatchUsers")
    @ApiOperation(value = "批量删除用户信息")
    public ResultFormat delBatchLink(Integer state, @RequestParam(value = "userId[]") Integer[] userId) {
        logger.info("state={}", state);

        Users users = new Users();
        users.setState(1);
        boolean updateById = false;
        for (Integer integer : userId) {
            users.setUserId(integer);
            updateById = usersService.updateById(users);
        }
        if (updateById) {
            return ResultUtil.success();
        }
        return ResultUtil.error(100, "修改失败");
    }


}
